<template>
  <div class="qingwu">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('kernel.notice.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-form-item :label="i18n('kernel.notice.category.title')" prop="category_id">
            <el-select v-model="dataForm.category_id" :placeholder="i18n('common.please_select') + i18n('kernel.notice.category.title')">
              <el-option v-for="(v, k) in categoryList" :label="v.title" :key="k" :value="v.id"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item :label="i18n('kernel.notice.content')" prop="content">
            <el-input type="textarea" :placeholder="i18n('common.please_input') + i18n('kernel.notice.content')" v-model="dataForm.content"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button
              v-if="isAuth('kernel:notice:handle')"
              type="primary"
              @click="dataFormSubmit(formRef)">
              {{ i18n('common.confirm') }}
            </el-button>
            <el-button @click="resetForm(formRef)">
              {{ i18n('common.reset') }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="notice-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('notice')
  const categoryList = ref([])
  const dataForm = reactive({
    id: 0,
    category_id: '',
    content: '',
  })

  const dataRule = reactive({
    content: [{
      required: true,
      message: i18n('kernel.notice.rules.content.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    dataFormSubmit,
    resetForm
  } = useForm(model.value, dataForm)

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/notice/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.category_id = data.data.category_id
            dataForm.content = data.data.content
          }
        })
      }
    })
  }

  const loadCategoryList = () => {
    $http({
      url: $http.adornUrl('/notice/category/select'),
      method: 'get',
      params: $http.adornParams({
        type: 1,
      }),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        categoryList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  onMounted(() => {
    init(formRef)
    loadCategoryList()
  })
</script>
